//author:jxx

import { set } from "shelljs";

//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let extension = {
    components: {//动态扩充组件或组件路径
        //表单header、content、footer对应位置扩充的组件
        gridHeader:'',//{ template: "<div>扩展组xx件</div>" },
        gridBody: '',
        gridFooter: '',
        //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
        modelHeader: '',
        modelBody: '',
        modelFooter: ''
    },
    buttons: {view: [], box:[],  detail:[]},//扩展的按钮
    methods: {//事件扩展
        empty(){
            this.editFormFields.uiType = "";
          },
        getFormOption(field) {
            let option;
            this.editFormOptions.forEach(x => {
                x.forEach(item => {
                    if (item.field == field) {
                        option = item;
                    }
                })
            })
            return option;
        },
       onInit() {
        this.rowKey="typeName";
        this.maxBtnLength = 5;
        this.columns.push({
            title: '操作',
            fixed: 'right',
            width: 140,
            render: (h, { row, column, index }) => {
                return h(
                    "div", { style: {} }, [
                        h(
                            "a", {
                                props: {},
                                style: {},
                                on: {
                                    click: (e) => {
                                        //this.$Message.info(row.accountsReceivable);
                                        // e.stopPropagation()
                                        //console.log(this.$refs.gridHeader)
                                        this.linkData(row);
                                    }
                                }
                            },
                            "编辑"
                        ),
                        
                    ])
            }
        })
        this.$store.getters.data().reportPrice = this;
        this.editFormFileds.parentID = [];
        var selectOption = this.getFormOption('parentID');
        selectOption.type = "cascader";
        selectOption.changeOnSelect = true
        //初始一个空的数据源(必须)
        selectOption.data=[];
        selectOption.onChange = (val, item) => {  
        let productname="";
        let productnames="";
        let prodctcode="";
        let ces = 0;
        for(let i=item.length-1;i>=0;i--){
            ces++;
          if(i==item.length-1){productnames+=item[i].label;prodctcode+=item[i].value};
          if(i==0){productname+=item[i].label;prodctcode+=item[i].value};
        }
        // if(productname!=null&&productnames!=null){
        //     this.editFormFileds.parentName=productname+("/")+productnames;
        // }else if(productname!=null){
        //     this.editFormFileds.parentName=productname;
        // }
         //判断如果只循环一次进入该判断，如果大于一次就是进入第二次判断
         if(ces==1){
            this.editFormFileds.parentName=productname;
         }else if(ces>1){
            this.editFormFileds.parentName=productname+("/")+productnames;
         }

       // if(this.editFormFileds.typeName==''){
         // this.editFormFileds.parentName=productname;
        //  this.editFormFileds.typeCode=prodctcode;//}
          //this.getcolumn(val);
          //this.$set(selectOption, "disabled", true);
      }  
        // console.log(parentID);
       },
       loadTreeChildren(tree, treeNode, resolve) { //加载子节点
        let url=`api/producttypes/getTreeTableChildrenData?typeId=${tree.id}`;
        this.http.post(url,{}).then(result=>{
          resolve(result.rows)
        })
        },
        searchBefore(params){//判断加载根节点或子节点
            //没有查询条件，默认查询返回所有根节点数据
            if (!params.wheres.length) {
            params.value=1;
            }
            return true;
        },
       getFormOption(field) {
        let option;
        this.editFormOptions.forEach(x => {
            x.forEach(item => {
                if (item.field == field) {
                    option = item;
                }

            })
        })
        return option;
        },
       rowClick({ row, column, event }) { //查询界面table点击行选中当前行
        //console.log(row,column,event);
        this.$refs.table.$refs.table.toggleRowSelection(row);
        return true;
       },
       modelOpenBefore(){
        var selectOption = this.getFormOption('parentID');
        selectOption.data=[];
          //从后台接口返回数据源,格式见iview组件result(建议在modelOpenBefore方法中设置数据源)
            this.http.post("api/producttypes/GetProtypeList6").then(result=>{
              result.result.forEach(item=>{
                  selectOption.data.push(item)
              })
          })

          this.editFormOptions.forEach(x => {
            x.forEach(item => {
              if (item.field == "uiType") {
                item.data = item.data.filter(x => { return x.key != "ui003"&&x.key!="ui002"&&x.key!="ui001" });
              }
            })
          })
      },
       modelOpenAfter() {
        //获取当前弹出框是新建还是编辑状态
        let isEDIT = this.currentAction == this.const.EDIT;
        if(isEDIT){
            var Code = this.getFormOption('attributes');
            this.$set(Code, "disabled", true)
            var Set = this.getFormOption('parentName');
            this.$set(Set,"disabled",true)
        }
        //设置让父级类型显示在添加或新建
        var parentIDOption = this.getFormOption("parentID");
        var parentNameOption = this.getFormOption("parentName");
        if(!isEDIT)
        {
          this.$set(parentIDOption, "hidden", false)
          
        }else{
          this.$set(parentIDOption, "hidden", true)
         // this.editFormFileds.parentID=[];
          
        }
        if(isEDIT){
          this.$set(parentNameOption, "hidden", false)

        }else{
          this.$set(parentNameOption, "hidden", true)
          this.editFormFileds.parentName=[];
        }
        //设置将产品名称转换成编码
        var selectOption = this.getFormOption('typeName');  
        if(!isEDIT) {
            selectOption.onInput = (val) => {    
            this.editFormFields.typeCode=this.base.chineseToPinYin(val);
            }
        } 
        var Code = this.getFormOption('typeCode');
        this.$set(Code, "disabled", true)
        //如果为编辑状态，父级类型不可修改
        this.editFormOptions.forEach(item=>{
            item.forEach(x=>{
                //如果是编辑,库存设置为只读
                if(x.field=="parentName"){
                    //disabled是editformOptions的动态属性，这里只能通过this.$set修改值
                    this.$set(x,"disabled",isEDIT)
                  
                }
                if(x.field == "attributes"){
                    this.$set(x,"disabled",isEDIT)
                }
            })
        })


        ///测试数据暂时不可删除
        // var selectOption = this.getFormOption('typeName');  
        // if(!isEDIT) {
        //     selectOption.onInput = (val) => {    
        //     this.editFormFields.typeCode=this.base.chineseToPinYin(val);
        //     }
        // } 
        // var Code = this.getFormOption('typeCode');
        // this.$set(Code, "disabled", true)
        // var attributes = this.getFormOption('attributes');
        // this.$set(attributes, "disabled", isEDIT)
        }
    }
};
export default extension;